#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <ctype.h>

int
val(const char ch)
{
	if (ch >= '0' && ch <= '9')      return (ch - '0');
	else if (ch >= 'A' && ch <= 'Z') return (10 + ch - 'A');

	return 0;
}

int
main(int argc, char **argv)
{
	int base, res = 0, negative = 0;
	int c;

	scanf("%d", &base);
	while (c = fgetc(stdin), c != EOF) {
		if (isspace(c))
			continue;

		if (c == '-')
			negative = 1;
		else
			res = res * base + val(toupper(c));
	}

	printf("%d\n", res * (negative ? -1 : 1));

	return 0;
}
